ALV-Grid mit Timer

In diesem Beispiel wird ein ALV-Grid aufgerufen, das jede Sekunde einen anderen Eintrag aus einer Tabelle anzeigt.

Gelesen wird die komplette Ländertabelle. Im ALV-Grid werden dann nacheinander – Timer gesteuert – die einzelnen Einträge angezeigt.

Varianten

Untenstehend zwei Varianten des gleichen Programms. Das erste ist ein “Standardprogramm” mit Eventhandler für den Timer. Die zweite Variante ist komplett objektorientiert. In dieser Variante fehlen die PAI- und PBO-Module. Bitte aus der ersten Variante kopieren!

Programminfos

Im Programm muss das Dynpro 500 mit einem “Custom Container” angelegt werden. Der Container muss “CC” heissen.

Als OK-Code im Dynpro nehmen wir “SY-UCOMM”.

Zusätzlich muss der GUI-Status “500” angelegt werden. Die Funktionen “F3”, SHIFT-F3 und F12 müssen mit dem Kommando “BACK” definiert werden.

Coding 1

REPORT  ZZ_ALV_TIMER_DEMO.

CLASS: gc_evt_handler DEFINITION DEFERRED.

*** data
DATA: gt_t005t       TYPE STANDARD TABLE OF t005t,
      gt_t005tx      TYPE STANDARD TABLE OF t005t,
      gv_index       TYPE i,
      gr_alv_grid    TYPE REF TO cl_gui_alv_grid,
      gr_evt_handler TYPE REF TO gc_evt_handler,
      gr_timer       TYPE REF TO cl_gui_timer,
      gr_cont        TYPE REF TO cl_gui_custom_container.

FIELD-SYMBOLS <t005t> TYPE t005t.

*———————————————————————-*
* CLASS gc_evt_handler DEFINITION
*———————————————————————-*
CLASS gc_evt_handler DEFINITION FINAL.

  PUBLIC SECTION.

    METHODS handle_when_done FOR EVENT finished OF cl_gui_timer.

ENDCLASS. “gc_evt_handler DEFINITION

*———————————————————————-*
* CLASS gc_evt_handler IMPLEMENTATION
*———————————————————————-*

CLASS gc_evt_handler IMPLEMENTATION.


  METHOD handle_when_done.

*** skip to next entry
    PERFORM get_next.
*** refresh grid
    gr_alv_grid->refresh_table_display( ).
*** start time again
    gr_timer->run( ).

  ENDMETHOD. “handle_when_done

ENDCLASS. “gc_evt_handler IMPLEMENTATION

*———————————————————————-*
START-OF-SELECTION.

*** get all entries from table
  SELECT * FROM t005t INTO TABLE gt_t005t WHERE spras = sy-langu.

*** show first entry of table
  PERFORM get_next.

*** call grid
  CALL SCREEN ‘0500’.

*&———————————————————————*
*&      Form  get_next
*&———————————————————————*
FORM get_next.

*** skip to next entry
  ADD 1 TO gv_index.
  IF gv_index > LINES( gt_t005t ).
    gv_index = 1.
  ENDIF.
*** read next entry
  READ TABLE gt_t005t ASSIGNING <t005t> INDEX gv_index.
*** clear ALV-table
  CLEAR gt_t005tx.
*** display next entry
  APPEND <t005t> TO gt_t005tx.

ENDFORM.                    “get_next

*&———————————————————————*
*& Form create_alv
*&———————————————————————*
FORM create_alv.

  CHECK gr_cont IS INITIAL.

*** create container
  CREATE OBJECT gr_cont
    EXPORTING
      container_name = ‘CC’.

*** create grid
  CREATE OBJECT gr_alv_grid
    EXPORTING
      i_parent = gr_cont.

*** display grid
  CALL METHOD gr_alv_grid->set_table_for_first_display
    EXPORTING
      i_structure_name = ‘T005T’
    CHANGING
      it_outtab        = gt_t005tx.


ENDFORM. “create_alv

*&———————————————————————*
*& Module STATUS_0500 OUTPUT
*&———————————————————————*

MODULE status_0500 OUTPUT.

  SET PF-STATUS ‘500’.
  PERFORM create_alv.
  PERFORM init_timer.

ENDMODULE. ” STATUS_0500 OUTPUT

*&———————————————————————*
*& Module USER_COMMAND_0500 INPUT
*&———————————————————————*

MODULE user_command_0500 INPUT.

  CASE sy-ucomm.
    WHEN ‘BACK’.
      SET SCREEN 0. LEAVE SCREEN.
  ENDCASE.

ENDMODULE. ” USER_COMMAND_0500 INPUT

*&———————————————————————*
*& FORM init_timer
*&———————————————————————*

FORM init_timer.

  CHECK gr_timer IS INITIAL.

*** create timer control
  CREATE OBJECT gr_timer.

*** create event handler
  CREATE OBJECT gr_evt_handler.

*** Register event handler
  SET HANDLER gr_evt_handler->handle_when_done FOR gr_timer.

*** Set value according to your requirement
  gr_timer->interval = 1.

*** Start timer
  gr_timer->run( ).

ENDFORM. ” INIT_TIMER

Coding 2 – Objektorientiert

REPORT ZZ_ALV_TIMER_DEMO_OO.

CLASS gc_application DEFINITION DEFERRED.

DATA gr_application TYPE REF TO gc_application.

*———————————————————————-*
*       CLASS gc_application DEFINITION
*———————————————————————-*

CLASS gc_application DEFINITION CREATE PUBLIC.


  PUBLIC SECTION.

    DATA: gt_t005t       TYPE STANDARD TABLE OF t005t,
          gt_t005tx      TYPE STANDARD TABLE OF t005t,
          gv_index       TYPE i,
          gr_alv_grid    TYPE REF TO cl_gui_alv_grid,
          gr_timer       TYPE REF TO cl_gui_timer,
          gr_cont        TYPE REF TO cl_gui_custom_container.

    METHODS get_next.
    METHODS constructor.
    METHODS create_alv.
    METHODS init_timer.
    METHODS handle_when_done FOR EVENT finished OF cl_gui_timer.

ENDCLASS.                    “gc_application DEFINITION

*———————————————————————-*
*       CLASS gc_application IMPLEMENTATION
*———————————————————————-*

CLASS gc_application IMPLEMENTATION.

  METHOD handle_when_done.

**** skip to next entry
    get_next( ).
**** refresh grid
    gr_alv_grid->refresh_table_display( ).
**** start timer again
    gr_timer->run( ).

  ENDMETHOD. “handle_when_done

  METHOD init_timer.

    CREATE OBJECT gr_timer.

*** Register event handler
    SET HANDLER handle_when_done FOR gr_timer.

*** Set value according to your requirement
    gr_timer->interval = 1.

*** Start timer
    gr_timer->run( ).

  ENDMETHOD.                    “init_timer

  METHOD get_next.

    FIELD-SYMBOLS <t005t> TYPE t005t.

*** skip to next entry
    ADD 1 TO gv_index.
    IF gv_index > LINES( gt_t005t ).
      gv_index = 1.
    ENDIF.
*** read next entry
    READ TABLE gt_t005t ASSIGNING <t005t> INDEX gv_index.
*** clear ALV-table
    CLEAR gt_t005tx.
*** display next entry
    APPEND <t005t> TO gt_t005tx.

  ENDMETHOD.                    “get_next

  METHOD create_alv.

*** create if not bound
    CHECK gr_cont IS INITIAL.

*** create container
    CREATE OBJECT gr_cont
      EXPORTING
        container_name = ‘CC’.

*** create grid
    CREATE OBJECT gr_alv_grid
      EXPORTING
        i_parent = gr_cont.

*** display grid
    CALL METHOD gr_alv_grid->set_table_for_first_display
      EXPORTING
        i_structure_name = ‘T005T’
      CHANGING
        it_outtab        = gt_t005tx.

  ENDMETHOD.                    “create_alv

  METHOD constructor.

*** get all entries from table
    SELECT * FROM t005t INTO TABLE gt_t005t WHERE spras = sy-langu.

*** show first entry of table
    get_next( ).
*** init timer
    init_timer( ).

  ENDMETHOD.                    “constructor

ENDCLASS.                    “gc_application IMPLEMENTATION

*———————————————————————-*
START-OF-SELECTION.

*** create application class
  CREATE OBJECT gr_application.

*** call grid
  CALL SCREEN ‘0500’.

 

 

Enno Wulff
Letzte Artikel von Enno Wulff (Alle anzeigen)

Leave a Comment